package com.itheima.controller;

import com.itheima.pojo.Emp;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Result;
import com.itheima.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@RestController
@RequestMapping("/emps")
@Slf4j
public class EmpController {

    @Autowired
    private EmpService empService;

    /*    @GetMapping
        public Result page(@RequestParam(defaultValue = "1") Integer page
                , @RequestParam(defaultValue = "10") Integer pageSize) {
            //记录日志
            log.info("分页查询员工信息,接收到的参数为:{},{}", page, pageSize);
            //调用业务层的分页查询功能
            PageBean pageBean = empService.page(page, pageSize);
            //响应，把数据返回给前端
            return Result.success(pageBean);*/
    //条件分页查询
    @GetMapping
    public Result page(
            @RequestParam(defaultValue = "1") Integer page
            , @RequestParam(defaultValue = "10") Integer pageSize
            , String name
            , Short gender
            , @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin
            , @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        //记录日志
        log.info("分页查询员工信息,接收到的参数为:{},{},{},{},{},{}"
                , page, pageSize, name, gender, begin, end);
        //调用业务层的分页查询功能
        PageBean pageBean = empService.page(page, pageSize, name, gender, begin, end);
        //响应，把数据返回给前端
        return Result.success(pageBean);
    }

    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids) {
        empService.delete(ids);
        return Result.success();
    }

    @PostMapping
    public Result add(@RequestBody Emp emp) {
        //打日志
        log.info("新增员工信息，接收到的参数为：{}", emp);
        //调用业务层的新增功能
        empService.add(emp);
        //返回响应
        return Result.success();
    }

    //根据id查询员工
    @GetMapping("/{id}")
    public Result getById(@PathVariable Number id) {
        Emp emp = empService.getById(id);
        return Result.success(emp);
    }

    //修改员工
    @PutMapping
    public Result update(@RequestBody Emp emp) {
        empService.update(emp);
        return Result.success();
    }
}
